package com.qf.java2007.web.controller;

import com.qf.java2007.pojo.Reader;
import com.qf.java2007.pojo.ReaderInfo;
import com.qf.java2007.service.AdminService;
import com.qf.java2007.service.ReaderCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2021/3/8 20:27
 * @Version 1.0
 */
@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private ReaderCardService readerCardService;

    /**
     * 显示头部导航栏
     * @return
     */
    @RequestMapping({"/admin_header.html","/bookinfo/admin_header.html"})
    public String adminHeader(){
        return "admin_header";
    }

    /**
     * 到管理员主界面
     * @return
     */
    @GetMapping("/admin_main.html")
    public String adminMain(){
        return "admin_main";
    }


    /**
     * 显示所有读者
     * @return
     */
    @GetMapping("/allreaders.html")
    public ModelAndView allReadersInfo(){

        List<ReaderInfo> readerInfos = readerCardService.findAll();
        ModelAndView mav=new ModelAndView("admin_readers");
        mav.addObject("readerInfos",readerInfos);
        return mav;
    }

    /**
     * 增加读者
     * @param reader
     * @return
     */
    @PostMapping("/addReader")
    public ModelAndView addReader(Reader reader){

        adminService.addRreaderInfo(reader);
        ModelAndView mav=new ModelAndView("admin_readers");

        List<ReaderInfo> readerInfos = readerCardService.findAll();
        mav.addObject("readerInfos",readerInfos);
        return mav;
    }
    //@PostMapping("/addReader")
    //public ModelAndView addReader(Object reader){
    //
    //    adminService.addReader(reader);
    //    ModelAndView mav=new ModelAndView("admin_readers");
    //
    //    return mav;
    //}
    @GetMapping("/admin_repasswd.html")
    public ModelAndView adminRepasswd(){

        //List<ReaderInfo> readerInfos = readerCardService.findAll();
        ModelAndView mav=new ModelAndView("admin_repasswd");
        //mav.addObject("readerInfos",readerInfos);
        return mav;
    }

    /**
     * 更新密码
     * @param
     * @return
     */
    @PostMapping("/updateAdminPwd")
    public ModelAndView updatePwd(String reNewPasswd,Long adminId){
        adminService.updatePwd(reNewPasswd,adminId);
        ModelAndView mav=new ModelAndView("admin_repasswd");
        return mav;
    }


    /**
     * 到读者信息修改界面
     * 数据回显
     * @param readerId
     * @return
     */
    @GetMapping("/reader_edit.html")
    public ModelAndView readerEdit(Long readerId){
        ReaderInfo readerInfo = readerCardService.findReaderCardById(readerId);

        //List<ReaderInfo> readerInfos = readerCardService.findAll();

        ModelAndView mav=new ModelAndView("admin_reader_edit");
        mav.addObject("readerInfo",readerInfo);
        return mav;
    }

    /**
     * 更新借阅人信息
     * @param readerInfo
     * @return
     */

    @PostMapping("/updateReader")
    public ModelAndView update(ReaderInfo readerInfo){
        readerCardService.update(readerInfo);
        List<ReaderInfo> readerInfos = readerCardService.findAll();


        ModelAndView mav = new ModelAndView("admin_readers");
        mav.addObject("succ", "修改成功！！！");
        mav.addObject("readerInfos",readerInfos);
        return mav;
    }

    /**
     * 删除借阅人信息
     * @param readerId
     * @return
     */
    @GetMapping("/reader_delete.html")
    public ModelAndView readerDelete(Long readerId){

        adminService.deleteReader(readerId);
        List<ReaderInfo> readerInfos = readerCardService.findAll();
        ModelAndView mav=new ModelAndView("admin_readers");
        mav.addObject("readerInfos",readerInfos);
        return mav;
    }
}
